import {
    pushTarget,
    popTarget
} from './dep.js'

let id = 0
class Watcher {
    constructor(vm, exprOrFn, callback, options) {
        this.vm = vm;
        this.callback = callback;
        this.options = options;
        this.id++
        this.getter = exprOrFn; // 将内部传过来的回调函数 放到getter属性上
        this.deps = [];
        this.depsId = new Set();
        this.get();
    }
    addDep(dep) {
        let id = dep.id;
        if (!this.depsId.has(id)) {
            this.depsId.add(id);
            this.deps.push(dep);
            console.log(1)
            dep.addSub(this);
        }
    }
    update() {
        this.get();
    }
    get() {
        pushTarget(this)
        this.getter(); // 执行 vm._update(vm._render());
        popTarget()
    }
}

export default Watcher